<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../vue.js"></script>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        h1{
            height: 48px;
            font-size: 26px;
            text-align: center;
            line-height: 48px;
            background: gainsboro;
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            z-index: 100;
        }

        li{
            list-style: none;
            display: flex;
            padding: 10px 0;
            border-bottom: 1px solid black;
        }
        li>input{
            width: 40px;
        }
        li>#shop{
            flex: 1;
            display: flex;
        }
        li>#shop>img{
            width: 80px;
            height: 120px;
            padding:0 20px;
        }
        li>#shop>#shop-item{
            flex: 1;
        }
        #all-shop{
            height: 48px;
            background: gainsboro;
            position: fixed;
            bottom: 0;
            left: 0;
            z-index: 100;
            right: 0;
        }
        #shopping-cart{
            height: calc(100vh - 48px - 48px);
            overflow: auto;
            margin-top: 48px;
        }
    </style>
</head>
<body>
    <div id="app">
        <h1>购物车</h1>
        <div id="shopping-cart">
            <ul>
                <li v-for=" (item,index) in list">
                    <input type="checkbox" v-model="item.isChecked">
                    <div id="shop">
                        <img :src="item.show.img">
                        <div id="shop-item">
                            <h4>{{item.title}}</h4>
                            <p>
                                <span>{{item.price}}</span>
                                <button type="button" @click="item.num++">+</button>
                                <span>{{item.num}}</span>
                                <button type="button" @click="number(index)">-</button>
                            </p>
                        </div>
                    </div>
                </li>
            </ul>
        </div>
        <div id="all-shop">
            {{allNum}}件商品，{{allPrice | toFixed}}元
        </div>
    </div>
    <script src="./1.js"></script>
    <script>
        let app = new Vue({
            el:"#app",
            data:{ 
                list:[],
            },
            created() {
                list.data.list.forEach((item)=>{
                    item.num = 1;
                    item.isChecked = true;
                })
                this.list = list.data.list;
            },
            methods:{ //方法
                number(index){
                    if(this.list[index].num > 1){
                        this.list[index].num--
                    }
                }
            },
            computed:{ //计算属性
                allNum(){
                    let shopArr = this.list.filter(item => item.isChecked)
                    return shopArr.reduce((a,b)=>{
                        return a + b.num
                    },0)
                },
                allPrice(){
                    let shopArr = this.list.filter(item => item.isChecked)
                    return shopArr.reduce((a,b)=>{
                        return a + b.num*b.price
                    },0)
                }
            },
            filters:{ //筛选器
                toFixed(v){
                    return v.toFixed(2)
                }
            }
        })
    </script>
</body>
</html>